var app = getApp();

var face_token = '';
var sImg;
var sImageName;
var avatarUrl;
var nickName;

const APP_ID = 'wxb32df375196aa830'; //输入小程序appid  
const APP_SECRET = '83992cba8aa3b84abdd718ddb871b950'; //输入小程序app_secret  
var OPEN_ID = '' //储存获取到openid  
var SESSION_KEY = '' //储存获取到session_key 

const io = require('../../utils/weapp.socket.io.dev.js');
Page({
  data: {
    backgroundName1,
    backgroundName2,
    colorName1,
    colorName2,
    displayName1,
    displayName2,
    sImage: '/images/head.png',
  },
  onLoad: function () {
    var that = this
    getOpenIdTap(that);
    wx.login({
      success: function () {
        wx.getUserInfo({
          success: function (res) {
            var simpleUser = res.userInfo;
            sImg = simpleUser.avatarUrl
            app.globalData.sName = simpleUser.nickName
            app.globalData.sImg = simpleUser.avatarUrl
            that.setData({
              nickName: simpleUser.nickName,
              sImg: simpleUser.avatarUrl,
            })
          }
        });
      }
    });
  },

  formSubmit_signIn: (e) => {

    var _tId = e.detail.value.sId
    var _tPassword = e.detail.value.sPassword
    if (_tId.length != 6) {
      wx.showModal({
        title: '提示',
        content: '请输入正确的6位教工号',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else if (_tPassword.length < 6) {
      wx.showModal({
        title: '提示',
        content: '密码长度不得少于6位',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      wx.request({
        url: "https://geek-team.xin/LAR/tLogin",
        method: 'POST',
        data: {
          tId: _tId,
          tPassword: _tPassword,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: (res) => {
          console.log(res)
          if (res.data.result == true) {
            wx.showToast({
              title: '登录成功！',
              icon: 'success',
            })
            app.globalData.sId = e.detail.value.sId;
            app.globalData.isStudent = false;
            wx.navigateBack({
              delta: 1
            })

            var socket;
            app.globalData.socket = (socket = io(
              `https://geek-team.xin?sId=${app.globalData.sId}&isStudent=${app.globalData.isStudent}`,
            ))
            console.log(app.globalData.socket)
            console.log(app.globalData.socket)
            socket.on('connect', function () {
              console.log('You joined')
            });
            socket.on('connect_error', function (d) {
              console.log(`connect_error: ${d}`)
            });
          } else if (res.data.message === "password is wrong") {
            console.log()
            wx.showModal({
              title: '登录失败',
              content: "密码错误，请重新输入",
            })
          } else {
            wx.showModal({
              title: '登录失败',
              content: "没有该用户，请先注册",
            })
          }
        },
        fail: (e) => console.log(`${e}`)
      })

    }

  },
  formSubmit_register: (e) => {
    var _data = e.detail.value;
    console.log(_data)
    if (_data.sId.length != 8) {
      wx.showModal({
        title: '提示',
        content: '请输入正确的8位数学号',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else if (_data.sPassword.length < 6) {
      wx.showModal({
        title: '提示',
        content: '密码长度不得少于6位',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      wx.request({
        url: 'https://geek-team.xin/LAR/sRegister',
        method: 'POST',
        data: {
          tId: _data.sId,
          tPassword: _data.sPassword,
          openid: OPEN_ID,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: (res) => {
          console.log(res)
          if (res.data.result == true) {
            wx.showToast({
              title: '注册成功',
              icon: 'success',
            })
            //跳转登录
            that.setData({
              background1: '#77c9d4',
              background2: '#ffffff',
              color1: '#f2ffff',
              color2: '#1a1a1a',
              display1: 'block',
              display2: 'none',
            })
          } else {
            wx.showToast({
              title: `注册失败${res.data.massage}`,
              image: '/images/error.png',
            })
          }
        },
        fail: (e) => console.log(`${e}`)
      })
    }

  },
  changeToRegister() {
    this.setData({
      background1: '#ffffff',
      background2: '#77c9d4',
      color1: '#1a1a1a',
      color2: '#f2ffff',
      display1: 'none',
      display2: 'block'
    })
  },
  /** 
   * 跳转登录
   */
  changeToSignin() {
    this.setData({
      background1: '#77c9d4',
      background2: '#ffffff',
      color1: '#f2ffff',
      color2: '#1a1a1a',
      display1: 'block',
      display2: 'none',
    })
  }
})
/*
 * 获取openid的
 */
function getOpenIdTap(that) {
  wx.login({
    success: function (res) {
      console.log(res)
      wx.request({
        //获取openid接口  
        // url: 'https://api.weixin.qq.com/sns/jscode2session',
        url: 'https://geek-team.xin/authorize',
        data: {
          // appid: APP_ID,
          // secret: APP_SECRET,
          js_code: res.code,
          // grant_type: 'authorization_code'
        },
        method: 'GET',
        success: function (res) {
          console.log(res.data)
          OPEN_ID = res.data.openid; //获取到的openid  
          SESSION_KEY = res.data.session_key; //获取到session_key  
          that.setData({
            openid: res.data.openid.substr(0, 10) + '********' + res.data.openid.substr(res.data.openid.length - 8, res.data.openid.length),
            session_key: res.data.session_key.substr(0, 8) + '********' + res.data.session_key.substr(res.data.session_key.length - 6, res.data.session_key.length)
          })
        }
      })
    }
  })
}
/*
 * 跳转注册
 */

/** 
 * 样式
 */
var backgroundName1 = {
  background1: '#77c9d4'
};
var backgroundName2 = {
  background2: '#ffffff'
};
var colorName1 = {
  color1: '#f2ffff'
};
var colorName2 = {
  color2: '#1a1a1a'
};
var displayName1 = {
  display1: 'block'
};
var displayName2 = {
  display2: 'none'
}